// mbv.cpp
#include <windows.h>
#include <iostream.h>
#include "registry.h"

const REG_DATA g_regData[] = {
    { "CLSID\\{10000001-1111-0000-0000-000000000001}", 0, "Marshal By Value" },
	{ "CLSID\\{10000001-1111-0000-0000-000000000001}\\InprocServer32", 0, (const char*)-1 }, 
	{ "CLSID\\{10000001-1111-0000-0000-000000000001}\\InprocServer32", "ThreadingModel", "Both" }, 
	{ "CLSID\\{10000001-1111-0000-0000-000000000001}\\ProgID", 0, "Component.MarshalByValue.1" },
	{ "CLSID\\{10000001-1111-0000-0000-000000000001}\\VersionIndependentProgID", 0, "Component.MarshalByValue" },
	{ "Component.MarshalByValue", 0, "Marshal By Value" },
	{ "Component.MarshalByValue\\CLSID", 0, "{10000001-1111-0000-0000-000000000001}" },
	{ "Component.MarshalByValue\\CurVer", 0, "Component.MarshalByValue.1" },
	{ "Component.MarshalByValue.1", 0, "Marshal By Value" },
	{ "Component.MarshalByValue.1\\CLSID", 0, "{10000001-1111-0000-0000-000000000001}" },
	{ 0, 0, 0 }
};

// {10000001-1111-0000-0000-000000000001}
const CLSID CLSID_MarshalByValue = {0x10000001,0x1111,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};

HINSTANCE g_hInstance = 0;
long g_cLocks = 0;

interface INoAggregationUnknown
{
	virtual HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv)=0;
	virtual ULONG __stdcall AddRef_NoAggregation()=0;
	virtual ULONG __stdcall Release_NoAggregation()=0;
};

class CMarshalByValue : public IMarshal, public INoAggregationUnknown
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);

	// INoAggregationUnknown
	ULONG __stdcall AddRef_NoAggregation();
	ULONG __stdcall Release_NoAggregation();
	HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv);

	// IMarshal
	HRESULT __stdcall GetUnmarshalClass(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags, CLSID* pClsid);
	HRESULT __stdcall GetMarshalSizeMax(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags, DWORD* pSize);
	HRESULT __stdcall MarshalInterface(IStream* pStream, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags);
	HRESULT __stdcall DisconnectObject(DWORD dwReserved);
	HRESULT __stdcall UnmarshalInterface(IStream* pStream, REFIID riid, void** ppv);
	HRESULT __stdcall ReleaseMarshalData(IStream* pStream);

	CMarshalByValue(IUnknown* pUnknownOuter);
	~CMarshalByValue();

private:
	ULONG m_cRef;
	IUnknown* m_pUnknownOuter;
};

CMarshalByValue::CMarshalByValue(IUnknown* pUnknownOuter) : m_cRef(1)
{
	InterlockedIncrement(&g_cLocks);
	if(pUnknownOuter != NULL)
		m_pUnknownOuter = pUnknownOuter;
	else
		m_pUnknownOuter = (IUnknown*)(INoAggregationUnknown*)this;
}

CMarshalByValue::~CMarshalByValue()
{
	cout << "CMarshalByValue::~CMarshalByValue()" << endl;
	InterlockedDecrement(&g_cLocks);
}

HRESULT CMarshalByValue::GetUnmarshalClass(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags, CLSID* pClsid)
{
	cout << "GetUnmarshalClass" << endl;
	IPersistStream* pPersistStream = 0;
	HRESULT hr = m_pUnknownOuter->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);
	if(FAILED(hr))
		cout << "QI failed for persist" << endl;

	pPersistStream->GetClassID(pClsid);

	pPersistStream->Release();
	return S_OK;
}

HRESULT CMarshalByValue::GetMarshalSizeMax(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags, DWORD* pSize)
{
	cout << "CMarshalByValue::GetMarshalSizeMax()" << endl;

	IPersistStream* pPersistStream = 0;
	m_pUnknownOuter->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);

	ULARGE_INTEGER size;
 	pPersistStream->GetSizeMax(&size);
	*pSize = size.LowPart;

	pPersistStream->Release();
	return S_OK;
}

HRESULT CMarshalByValue::MarshalInterface(IStream* pStream, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD dwFlags)
{
	AddRef();

	cout << "CMarshalByValue::MarshalInterface()" << endl;

	IPersistStream* pPersistStream = 0;
	m_pUnknownOuter->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);

	HRESULT hr = pPersistStream->Save(pStream, TRUE);

	pPersistStream->Release();

	return hr;
}

HRESULT CMarshalByValue::DisconnectObject(DWORD dwReserved)
    {
	cout << "DisconnectObject" << endl;
    return S_OK;;
    }

HRESULT CMarshalByValue::ReleaseMarshalData(IStream* pStream)
    {
	cout << "ReleaseMarshalData" << endl;
    return S_OK;
    }

HRESULT CMarshalByValue::UnmarshalInterface(IStream* pStream, REFIID riid, void** ppv)
    {
	cout << "IMarshal::UnmarshalInterface" << endl;

	IPersistStream* pPersistStream = 0;
	m_pUnknownOuter->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);

	pPersistStream->Load(pStream);

	pPersistStream->Release();
	
	return m_pUnknownOuter->QueryInterface(riid, ppv);
    }

HRESULT CMarshalByValue::QueryInterface_NoAggregation(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown)
	{
		cout << "CMarshalByValue::QueryInterface() for IUnknown returning " << this << endl;
		*ppv = (INoAggregationUnknown*)this;
	}
	else if(riid == IID_IMarshal)
	{
		cout << "CMarshalByValue::QueryInterface() for IMarshal returning " << this << endl;
		*ppv = (IMarshal*)this;
	}
	else 
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	((IUnknown*)(*ppv))->AddRef();
	return S_OK;
}

ULONG CMarshalByValue::AddRef_NoAggregation()
{
	return InterlockedIncrement((long*)&m_cRef);
}

ULONG CMarshalByValue::Release_NoAggregation()
{
	ULONG cRef = InterlockedDecrement((long*)&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

ULONG CMarshalByValue::AddRef()
{
	return m_pUnknownOuter->AddRef();
}

ULONG CMarshalByValue::Release()
{
	return m_pUnknownOuter->Release();
}

HRESULT CMarshalByValue::QueryInterface(REFIID riid, void** ppv)
{
	return m_pUnknownOuter->QueryInterface(riid, ppv);
}

class CFactory : public IClassFactory
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);

	// IClassFactory
	HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

	CFactory() : m_cRef(1) { InterlockedIncrement(&g_cLocks); }
	~CFactory() { InterlockedDecrement(&g_cLocks); }

private:
	ULONG m_cRef;
};

ULONG CFactory::AddRef()
{
	return InterlockedIncrement((long*)&m_cRef);
}

ULONG CFactory::Release()
{
	ULONG cRef = InterlockedDecrement((long*)&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown || riid == IID_IClassFactory)
	{
		cout << "CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
		*ppv = (IClassFactory*)this;
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CFactory::CreateInstance(IUnknown* pUnknownOuter, REFIID riid, void** ppv)
{
	if(pUnknownOuter != NULL && riid != IID_IUnknown)
		return CLASS_E_NOAGGREGATION;

	CMarshalByValue *pMarshalByValue = new CMarshalByValue(pUnknownOuter);
	if(pMarshalByValue == NULL)
		return E_OUTOFMEMORY;

	HRESULT hr = pMarshalByValue->QueryInterface_NoAggregation(riid, ppv);
	pMarshalByValue->Release_NoAggregation();
	return hr;
}

HRESULT CFactory::LockServer(BOOL bLock)
{
	if(bLock)
		InterlockedIncrement(&g_cLocks);
	else
		InterlockedDecrement(&g_cLocks);
	return S_OK;
}

HRESULT __stdcall DllCanUnloadNow()
{
	cout << "MarshalByValue DllCanUnloadNow() " << (g_cLocks == 0 ? "Yes" : "No") << endl;
	if(g_cLocks == 0)
		return S_OK;
	else
		return S_FALSE;
}

HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
{
	cout << "DllGetClassObject" << endl;
	
	if(clsid != CLSID_MarshalByValue)
		return CLASS_E_CLASSNOTAVAILABLE;

	CFactory* pFactory = new CFactory;
	if(pFactory == NULL)
		return E_OUTOFMEMORY;

	HRESULT hr = pFactory->QueryInterface(iid, ppv);
	pFactory->Release();
	return hr;
}

HRESULT __stdcall DllRegisterServer()
{
	char DllPath[MAX_PATH];
	GetModuleFileName(g_hInstance, DllPath, sizeof(DllPath));
	return RegisterServerEx(g_regData, DllPath);
}

HRESULT __stdcall DllUnregisterServer()
{
	return UnregisterServerEx(g_regData);
}

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
{
	g_hInstance = hInstance;
	return TRUE;
}